<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\store\shop;

use app\common\model\store\shop\Clerk as ClerkModel;
use cores\exception\BaseException;

/**
 * 商家门店店员模型
 * Class Clerk
 * @package app\api\model\store\shop
 */
class Clerk extends ClerkModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	/**
	 * 获取列表数据
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList(array $param = []): \think\Paginator {
		// 检索查询条件
		$filter = $this->getFilter($param);
		// 查询列表数据
		return $this->with(['avatarImage', 'shop'])
			->where($filter)
			->where('is_delete', '=', 0)
			->order(['create_time' => 'desc', $this->getPk()])
			->paginate(15);
	}
	public function getListAll(array $param = []) {
		// 检索查询条件
		$filter = $this->getFilter($param);
		// 查询列表数据
		return $this
			->where($filter)
			->where('is_delete', '=', 0)
		//	->where('work_status', '=', 10)
			->where('status', '=', 1)
			->order(['create_time' => 'desc', $this->getPk()])
			->select()->toArray();
	}
    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array {
        // 默认查询参数
        $params = $this->setQueryDefaultValue($param, [
            'shop_id' => 0, // 门店id
            'search' => '', // 搜索关键词: 姓名/手机号
            'status' => '', // 门店状态(0禁用 1启用)
        ]);
        // 检索查询条件
        $filter = [];
        // 门店id
        $params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int) $params['shop_id']];
        // 门店id
        $params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int) $params['shop_id']];
        // 门店状态
        is_numeric($params['status']) && $filter[] = ['status', '=', (int) $params['status']];
        // 搜索关键词
        !empty($params['search']) && $filter[] = ['real_name|mobile', 'like', "%{$params['search']}%"];
        return $filter;
    }
	/**
	 * 获取店员信息
	 * @param int $userId
	 * @return Clerk|array|null
	 * @throws BaseException
	 */
	public static function getClerkInfo(int $userId) {
		// 获取店员记录
		$clerk = static::detail(['user_id' => $userId, 'is_delete' => 0]);
		if (empty($clerk)) {
			throwError('很抱歉，您不是门店的店员');
		}
		if (!$clerk['status']) {
			throwError('很抱歉，当前店员状态已被禁用');
		}
		return $clerk;
	}

	/**
	 * 验证店员信息是否为指定门店的核销员
	 * @param Clerk $clerk 店员信息
	 * @param int $shopId 指定的门店ID
	 * @return bool
	 * @throws BaseException
	 */
	public static function checkAuthority(self $clerk, int $shopId): bool {
		if ($clerk['shop_id'] != $shopId) {
			throwError('很抱歉，当前店员不属于该门店，没有核销权限');
		}
		if (!$clerk['status']) {
			throwError('很抱歉，当前店员状态已被禁用');
		}
		return true;
	}
}